﻿using KongMing.ORM.EFCore.QueryModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KongMing.ORM.EFCore
{
    public class 基础查询
    {
        public static void NormalQueryList()
        {
            using (var db = new EFDBContext())
            {
                try
                {
                    var userInfo = db.UserInfo.ToList();
                    //1、查询所有
                    //var query_all = db.Queryable<Book>().ToList();
                    //var query_all = db.Book.ToList();

                    //2、查询单条
                    //var query_first = db.Queryable<Book>().First(x => x.Sn == 1);
                    //var query_first = db.Book.FirstOrDefault(x => x.Sn == 1);

                    //3、SqlServer 里面的withnolock
                    //var query_allNolock = db.Queryable<Book>().With(SqlWith.NoLock).ToList();

                    //4、根据主键查询单条
                    //var query_primaryKey = db.Queryable<Book>().InSingle("26d9bae8be154191a29599615358e912");
                    //var query_primaryKey = db.Book.Find("26d9bae8be154191a29599615358e912");

                    //7、* 查询实体 AS 数据实体 建立联系,查询需要的字段 *
                    //var query_as = db.Queryable<QueryBook>().AS("Book").ToList();

                    //8、 where 条件查询
                    //使用 lambda 表达式
                    //var query_where_lambda = db.Queryable<Book>().Where(x => x.IsDelete == 0 && x.Author == "刘慈欣").ToList();

                    //使用sql语句 和 SqlParameter 参数
                    //var query_where_param = db.Queryable<Book>().Where("isdelete=0 and author=@author", new { author = "刘慈欣" }).ToList();

                    //9、模糊查询
                    //var query_like = db.Queryable<Book>().Where(x => x.Title.Contains("图书")).ToList();

                    //10、in
                    string[] autors = new string[] { "刘慈欣", "罗贯中" };
                    //var query_in = db.Queryable<Book>().Where(x => autors.Contains(x.Author)).ToList();

                    //11、OrderBy 排序
                    //等同于sql：select top 10 * from Book order by inprice desc,sn asc
                    //var query_order1 = db.Queryable<Book>().OrderBy(x => x.InPrice, OrderByType.Desc).OrderBy(x => x.Sn, OrderByType.Asc).Take(10).ToList();
                    //var query_order2 = db.Queryable<Book>().OrderBy(" inprice desc,sn asc ").Take(10).ToList();
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
    }
}
